package com.ruoyi.web.controller.area;

import com.ruoyi.area.domain.Area;
import com.ruoyi.area.service.IAreaService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库区管理Controller
 * 
 * @author lh
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/area/area")
public class AreaController extends BaseController
{
    @Autowired
    private IAreaService areaService;

    /**
     * 查询库区管理列表
     */
    @PreAuthorize("@ss.hasPermi('area:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(Area area)
    {
        startPage();
        List<Area> list = areaService.selectAreaList(area);
        return getDataTable(list);
    }

    /**
     * 导出库区管理列表
     */
    @PreAuthorize("@ss.hasPermi('area:area:export')")
    @Log(title = "库区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Area area)
    {
        List<Area> list = areaService.selectAreaList(area);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        util.exportExcel(response, list, "库区管理数据");
    }

    /**
     * 获取库区管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('area:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(areaService.selectAreaById(id));
    }

    /**
     * 新增库区管理
     */
    @PreAuthorize("@ss.hasPermi('area:area:add')")
    @Log(title = "库区管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Area area)
    {
        area.setAreaNo(SeqNumberUtils.getHQNum());
        System.out.println("111222"+area);
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改库区管理
     */
    @PreAuthorize("@ss.hasPermi('area:area:edit')")
    @Log(title = "库区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Area area)
    {
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除库区管理
     */
    @PreAuthorize("@ss.hasPermi('area:area:remove')")
    @Log(title = "库区管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(areaService.deleteAreaByIds(ids));
    }

    /**
     * 获取仓库下的所有库区
     */
    @PreAuthorize("@ss.hasPermi('area:area:selectByPid')")
    @GetMapping( "selectByPid")
    public TableDataInfo selectByPid(Long id,String areaNo,String name)
    {
        startPage();
        List<Area> list = areaService.selectByPid(id,areaNo,name);
        return getDataTable(list);
    }
}
